package services;

import java.util.Iterator;
import java.util.LinkedList;

import objetos.ItemVendaObj;
import objetos.MarcaObj;
import objetos.ProdutoObj;

import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;

import classes.CarrinhoCompraImpl;
import classes.MarcaImpl;
import classes.ProdutoImpl;

public class MarcaService {

	public OMElement efetuarCadastro(OMElement element) {
		
		// Implementacao
		MarcaImpl mi = new MarcaImpl();
		
		// Cria elemento de retorno
		OMFactory factory = OMAbstractFactory.getOMFactory();
		OMNamespace ns_return = factory.createOMNamespace("return", "ret");
		OMElement return_element = factory.createOMElement("return", ns_return);
		
		// Objeto
		MarcaObj marca = new MarcaObj();
		
		// Seta objeto
		marca.loadOMElement(element);
		
		// Preenche retorno
		if ( mi.efetuarCadastro(marca) ) return_element.setText("true");
		else return_element.setText("false");	
		
		return return_element;
	}
	
	public OMElement atualizarCadastro(OMElement element) {
		// Implementacao
		MarcaImpl mi = new MarcaImpl();
		
		// Cria elemento de retorno
		OMFactory factory = OMAbstractFactory.getOMFactory();
		OMNamespace ns_return = factory.createOMNamespace("return", "ret");
		OMElement return_element = factory.createOMElement("return", ns_return);
		
		// Objeto
		MarcaObj marca = new MarcaObj();
		
		// Seta objeto
		marca.loadOMElement(element);
		
		// Preenche retorno
		if (mi.atualizarCadastro(marca)) return_element.setText("true");
		else return_element.setText("false");	
		
		return return_element;
	}
	
	public OMElement removerCadastro(OMElement element) {
		
		// Implementacao
		MarcaImpl pi = new MarcaImpl();
		
		// Cria elemento de retorno
		OMFactory factory = OMAbstractFactory.getOMFactory();
		OMNamespace ns_return = factory.createOMNamespace("return", "ret");
		OMElement return_element = factory.createOMElement("return", ns_return);
		
		if ( pi.removerCadastro(Integer.parseInt(element.getText())) ) return_element.setText("true");
		else return_element.setText("false");
		
		return return_element;
	}
	
	public OMElement buscarMarca(OMElement element) {
		
		OMFactory fac = OMAbstractFactory.getOMFactory();
		OMNamespace ns_ret = fac.createOMNamespace("return", "ret");
		OMElement return_element = fac.createOMElement("lista", ns_ret);
		
		MarcaImpl mi = new MarcaImpl();
		
		LinkedList<MarcaObj> list = mi.buscarMarca(element.getText());
		Iterator<MarcaObj> it = list.iterator();
		
		while(it.hasNext())
			return_element.addChild(it.next().toOMElement());

		return return_element;
	}
}
